﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections;

namespace Sinacor.Infra.Tools.ServiceRegistration.Web
{
    public partial class Services : Page, IServiceController
    {
        #region Attributes

        private ServiceController _controller;

        private const string TitleNode = "{0}";
        private const string TitleNodeRoot = "Módulos";
        private const string ImageRoot = "~/Images/root.gif";
        private const string ImageModule = "~/Images/modulo.gif";
        private const string ImageSubModule = "~/Images/submodulo.gif";
        private const string ImageService = "~/Images/service.jpg";
        private const string ImageMethod = "~/Images/method.gif";
        private const string ImageParameter = "~/Images/field.gif";
        private const string ImageAttribute = "~/Images/type.gif";
        private const string ImageDivergent = "~/Images/divergent.gif";
        private const string ItemDropDownList = ":: Selecione ::";
        private const string DeleteMessage = "return confirm('Deseja realmente excluir este item?');";
        private const string InsertMessage = "alert('Este item já existe!');";

        #endregion

        #region Events

        #region Load

        protected void Page_Load(object sender, EventArgs e)
        {
            GetServiceControllerInstance();

            if (!Page.IsPostBack && !ScriptManagerService.IsInAsyncPostBack)
            {
                _controller.SelectTypeListStatic();
                _controller.SelectAttributeListStatic();
                _controller.SelectModule();
                _controller.SelectProtocol();
                _controller.IsAttributePopupVisible = false;
                SearchTreeView();
            }
        }

        protected void ScriptManagerService_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            ScriptManagerService.AsyncPostBackErrorMessage = e.Exception.Message;
        }

        #endregion

        #region Buttons

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SearchTreeView();
        }

        protected void btnSearchPopupType_Click(object sender, EventArgs e)
        {
            SearchType();
            ShowModalType();
        }        

        protected void btnClear_Click(object sender, EventArgs e)
        {
            ClearFieldsSearch();
            SearchTreeView();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            KindVision kindVision = (KindVision)Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[0]);

            switch (kindVision)
            {
                case KindVision.Root:
                    if (string.IsNullOrEmpty(txtModuleIdEdit.Value)) InsertModule(); else UpdateModule();
                    break;
                case KindVision.Module:
                    if (string.IsNullOrEmpty(txtSubModuleIdEdit.Value)) InsertSubModule(); else UpdateSubModule();
                    break;
                case KindVision.SubModule:
                    if (string.IsNullOrEmpty(txtServiceIdEdit.Value)) InsertService(); else UpdateService();
                    break;
                case KindVision.Service:
                    if (string.IsNullOrEmpty(txtMethodIdEdit.Value)) InsertMethod(); else UpdateMethod();
                    break;
                case KindVision.Method:
                    if (string.IsNullOrEmpty(txtParameterIdEdit.Value)) InsertParameter(); else UpdateParameter();
                    break;
                case KindVision.Parameter:
                    if (string.IsNullOrEmpty(txtAttributeIdEdit.Value)) InsertAttribute(); else UpdateAttribute();
                    break;
                case KindVision.Attribute:
                    if (string.IsNullOrEmpty(txtAttributeIdEdit.Value)) InsertAttribute(); else UpdateAttribute();
                    break;
            };
        }

        protected void btnSavePopupType_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTypeIdEditPopup.Value))
                InsertType();
            else
                UpdateType();

            ShowModalType();
        }

        protected void btnSavePopupAttribute_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAttributeIdEditPopup.Value))
                InsertAttribute();
            else
                UpdateAttribute();

            ShowModalAttribute();
        }

        protected void btnNewPopupType_Click(object sender, EventArgs e)
        {
            ClearTypeFields();
            ShowModalType();
        }

        protected void btnNewPopupAttribute_Click(object sender, EventArgs e)
        {
            ClearAttributePopupFields();
            ShowModalAttribute();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            KindVision kindVision = (KindVision)Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[(int)KindVision.Root]);

            switch (kindVision)
            {
                case KindVision.Root:
                    ClearModuleFields();
                    ((IServiceController)this).ShowModuleList();
                    break;
                case KindVision.Module:
                    ClearSubModuleFields();
                    ((IServiceController)this).ShowSubModuleList();
                    _controller.GetNextSubModulePort();
                    break;
                case KindVision.SubModule:
                    ClearServiceFields();
                    ((IServiceController)this).ShowServiceList();
                    break;
                case KindVision.Service:
                    ClearMethodFields();
                    ((IServiceController)this).ShowMethodList();
                    break;
                case KindVision.Method:
                    ClearParameterFields();
                    ((IServiceController)this).ShowParameterList();
                    break;
                case KindVision.Parameter:
                    ClearAttributeFields();
                    ((IServiceController)this).ShowParameterList();
                    break;
                case KindVision.Attribute:
                    ClearAttributeFields();
                    ((IServiceController)this).ShowAttributeList();
                    break;
            };
        }

        protected void btnSearchReturnType_Click(object sender, EventArgs e)
        {
            ClearTypeFields();
            _controller.TypeList = new List<Entity.Type>();
            _controller.TypeList.AddRange(_controller.TypeListStatic.ToArray());
            ((IServiceController)this).ShowTypeList();
            ShowModalType();
        }

        protected void btnClosePopup_Click(object sender, EventArgs e)
        {
            _controller.IsAttributePopupVisible = false;
        }

        #endregion

        #region DropDownList

        protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlModule.SelectedIndex > 0)
            {
                _controller.SelectSubModule(Convert.ToInt32(ddlModule.SelectedItem.Value));
            }
            else
            {
                ddlSubModule.Items.Clear();
                ddlSubModule.Items.Insert(0, new ListItem(ItemDropDownList));
            }
        }

        #endregion

        #region TreeView

        protected void trvService_SelectedNodeChanged(object sender, EventArgs e)
        {
            KindVision kindVision = (KindVision)Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[(int)KindVision.Root]);
            int id = Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[(int)KindVision.Module]); 

            ShowKindVision(kindVision);

            switch (kindVision)
            {
                case KindVision.Root:
                    ClearModuleFields();
                    _controller.SelectModuleList();
                    break;
                case KindVision.Module:
                    ClearSubModuleFields();
                    _controller.SelectSubModuleList(id);
                    _controller.GetNextSubModulePort();
                    break;
                case KindVision.SubModule:
                    ClearServiceFields();
                    _controller.SelectServiceList(id);
                    break;
                case KindVision.Service:
                    ClearMethodFields();
                    _controller.SelectMethodList(id);
                    break;
                case KindVision.Method:
                    ClearParameterFields();
                    _controller.SelectParameterList(id);
                    break;
                case KindVision.Parameter:
                    ClearAttributeFields();
                    _controller.SelectAttributeList(FindTypeIdByParameterId(trvService.SelectedNode));
                    break;
                case KindVision.Attribute:
                    ClearAttributeFields();
                    _controller.SelectAttributeList(id);
                    break;
            };

            trvService.SelectedNode.Expand();
        }

        protected void trvService_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            KindVision kindVision = (KindVision)Convert.ToInt32(e.Node.Value.Split('|')[0]);

            if (_controller == null) GetServiceControllerInstance();

            if (_controller.TreeViewList != null)
            {
                switch (kindVision)
                {
                    case KindVision.Root:
                        PopulateNodes(null, e.Node.ChildNodes, kindVision);
                        break;
                    case KindVision.Module:
                        PopulateNodes(
                            _controller.TreeViewList.Find(m => m.ModuleId == Convert.ToInt32(e.Node.Value.Split('|')[1])).SubModules.ToArray(),
                            e.Node.ChildNodes, kindVision
                            );
                        break;
                    case KindVision.SubModule:
                        PopulateNodes(
                            _controller.TreeViewList.Find(m => m.ModuleId == Convert.ToInt32(e.Node.Value.Split('|')[2])).
                                SubModules.Find(s => s.SubModuleId == Convert.ToInt32(e.Node.Value.Split('|')[1])).Services.ToArray(),
                            e.Node.ChildNodes, kindVision
                            );
                        break;
                    case KindVision.Service:
                        PopulateNodes(
                            _controller.TreeViewList.Find(m => m.ModuleId == Convert.ToInt32(e.Node.Value.Split('|')[3])).
                                SubModules.Find(s => s.SubModuleId == Convert.ToInt32(e.Node.Value.Split('|')[2])).
                                    Services.Find(s => s.ServiceId == Convert.ToInt32(e.Node.Value.Split('|')[1])).Methods.ToArray(),
                            e.Node.ChildNodes, kindVision
                            );
                        break;
                    case KindVision.Method:
                        PopulateNodes(
                            _controller.TreeViewList.Find(m => m.ModuleId == Convert.ToInt32(e.Node.Value.Split('|')[4])).
                                SubModules.Find(s => s.SubModuleId == Convert.ToInt32(e.Node.Value.Split('|')[3])).
                                    Services.Find(s => s.ServiceId == Convert.ToInt32(e.Node.Value.Split('|')[2])).
                                        Methods.Find(m => m.MethodId == Convert.ToInt32(e.Node.Value.Split('|')[1])).Parameters.ToArray(),
                            e.Node.ChildNodes, kindVision
                            );
                        break;
                    case KindVision.Parameter:
                        PopulateNodes(  _controller.AttributeListStatic.FindAll(t => t.ParentTypeId == FindTypeIdByParameterId(e.Node)).ToArray(), 
                                        e.Node.ChildNodes, 
                                        kindVision);
                        break;
                    case KindVision.Attribute:
                        PopulateNodes(_controller.AttributeListStatic.FindAll(t => t.ParentTypeId == Convert.ToInt32(e.Node.Value.Split('|')[1])).ToArray(),
                                        e.Node.ChildNodes,
                                        kindVision);
                        break;
                };
            }
        }

        #endregion

        #region Data Grid

        protected void gdv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            KindVision kindVision = (KindVision)Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[0]);

            switch (kindVision)
            {
                case KindVision.Root:

                    DeleteModule(e.RowIndex);
                    ClearModuleFields();
                    ((IServiceController)this).ShowModuleList();

                    break;
                case KindVision.Module:

                    DeleteSubModule(e.RowIndex);
                    ClearSubModuleFields();
                    ((IServiceController)this).ShowSubModuleList();

                    break;
                case KindVision.SubModule:

                    DeleteService(e.RowIndex);
                    ClearServiceFields();
                    ((IServiceController)this).ShowServiceList();

                    break;
                case KindVision.Service:

                    DeleteMethod(e.RowIndex);
                    ClearMethodFields();
                    ((IServiceController)this).ShowMethodList();

                    break;
                case KindVision.Method:

                    DeleteParameter(e.RowIndex);
                    ClearParameterFields();
                    ((IServiceController)this).ShowParameterList();

                    break;

                case KindVision.Parameter:

                    DeleteAttribute(e.RowIndex);
                    ClearAttributeFields();
                    ((IServiceController)this).ShowAttributeList();

                    break;
                case KindVision.Attribute:
                    DeleteAttribute(e.RowIndex);
                    ClearAttributeFields();
                    ((IServiceController)this).ShowAttributeList();
                    break;
            };
        }

        protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && trvService.SelectedNode != null)
            {
                e.Row.Attributes.Add("onmouseover", "if (this.className != 'rowgridclick') this.className = 'rowgridhouver';");
                e.Row.Attributes.Add("onmouseout", "if (this.className != 'rowgridclick') this.className = 'rowgrid';");

                if (((GridView)sender).ID == "gdvTypePopup")
                {
                    e.Row.Attributes.Add("onclick", "SetItem(this, '7');");
                    e.Row.Attributes.Add("ondblclick", "SetItemPopup(this);");
                }
                else if (((GridView)sender).ID == "gdvAttributePopup")
                    e.Row.Attributes.Add("onclick", "SetItem(this, '8');");
                else
                    e.Row.Attributes.Add("onclick", "SetItem(this, '" + trvService.SelectedNode.Value.Split('|')[0] + "');");
                
                ((ImageButton)e.Row.Cells[e.Row.Cells.Count - 1].FindControl("btnDelete")).Attributes.Add("OnClick", DeleteMessage);

                if (((GridView)sender).ID == "gdvService")
                    e.Row.Cells[2].Text = e.Row.Cells[2].Text.Length > 70 ? e.Row.Cells[2].Text.Substring(0, 70) : e.Row.Cells[2].Text;

                if (((GridView)sender).ID == "gdvMethod")
                    if (e.Row.DataItem != null && ((Entity.Method)e.Row.DataItem).ReturnType.FullTypeName != "System.Void")
                        ((ImageButton)e.Row.FindControl("imgSearchAttributes")).Visible = EntityTypeHasAttributes(((Entity.Method)e.Row.DataItem).ReturnType);

                if (((GridView)sender).ID == "gdvParameter")
                    if (e.Row.DataItem != null && ((Entity.Parameter)e.Row.DataItem).Type != null)
                        ((ImageButton)e.Row.FindControl("imgSearchAttributes")).Visible = EntityTypeHasAttributes(((Entity.Parameter)e.Row.DataItem).Type);

                if (((GridView)sender).ID == "gdvTypePopup")
                    if (e.Row.DataItem != null && ((Entity.Type)e.Row.DataItem).TypeId != null)
                        ((ImageButton)e.Row.FindControl("imgSearchAttributes")).Visible = EntityTypeHasAttributes(((Entity.Type)e.Row.DataItem));

                if (((GridView)sender).ID.StartsWith("gdvAttribute"))
                    if (e.Row.DataItem != null && ((Entity.Attribute)e.Row.DataItem).Type != null)
                        ((ImageButton)e.Row.FindControl("imgSearchAttributes")).Visible = EntityTypeHasAttributes(((Entity.Attribute)e.Row.DataItem).Type);

                ((HtmlImage)e.Row.FindControl("imgItemDivergent")).Visible = ((Entity.EntityBase)e.Row.DataItem).IsItemDivergent;
            }
        }

        protected void gdv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Open")
            {
                ClearAttributePopupFields();
                _controller.IsAttributePopupVisible = true;
                _controller.AttributeParentTypeId = Convert.ToInt32(e.CommandArgument);
                _controller.SelectAttributeList(Convert.ToInt32(e.CommandArgument));
                ShowModalAttribute();
            }
        }       

        protected void gdv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            KindVision kindVision = (KindVision)Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[0]);

            ((GridView)sender).PageIndex = e.NewPageIndex;

            switch (kindVision)
            {
                case KindVision.Root:
                    ((IServiceController)this).ShowModuleList();
                    break;
                case KindVision.Module:
                    ((IServiceController)this).ShowSubModuleList();
                    break;
                case KindVision.SubModule:
                    ((IServiceController)this).ShowServiceList();
                    break;
                case KindVision.Service:
                    ((IServiceController)this).ShowMethodList();
                    break;
                case KindVision.Method:
                    ((IServiceController)this).ShowParameterList();
                    break;
                case KindVision.Parameter:
                    ((IServiceController)this).ShowAttributeList();
                    break;
                case KindVision.Attribute:
                    ((IServiceController)this).ShowAttributeList();
                    break;
            };
        }

        protected void gdvPopupType_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DeleteType(e.RowIndex);
            ClearTypeFields();
            ((IServiceController)this).ShowTypeList();
            ShowModalType();
        }

        protected void gdvPopupAttribute_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DeleteAttribute(e.RowIndex);
            ClearAttributePopupFields();
            ((IServiceController)this).ShowAttributeList();
            ShowModalAttribute();
        }

        protected void gdvPopupType_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           ((GridView)sender).PageIndex = e.NewPageIndex;
           ((IServiceController)this).ShowTypeList();
           ShowModalType();
        }

        protected void gdvPopupAttribute_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ((GridView)sender).PageIndex = e.NewPageIndex;
            ((IServiceController)this).ShowAttributeList();
            ShowModalAttribute();
        }

        #endregion

        #endregion

        #region Methods

        #region Session

        private void GetServiceControllerInstance()
        {
            if (Session["SessionServiceController"] != null)
            {
                _controller = (ServiceController)Session["SessionServiceController"];
                _controller.View = this;
            }
            else
            {
                _controller = new ServiceController(this);
                Session["SessionServiceController"] = _controller;
            }
        }

        #endregion

        #region Clear Methods

        private void ClearFieldsSearch()
        {
            ddlModule.SelectedIndex = 0;
            ddlModule_SelectedIndexChanged(null, null);
            chkService.Checked = false;
            chkMethod.Checked = false;
            chkParameter.Checked = false;
            txtPort.Text = string.Empty;
            txtService.Text = string.Empty;
            txtMethod.Text = string.Empty;
            txtParameter.Text = string.Empty;
            txtDescription.Text = string.Empty;
            txtRelatedTables.Text = string.Empty;
            txtType.Text = string.Empty;
        }

        private void ClearModuleFields()
        {
            txtModuleIdEdit.Value = string.Empty;
            txtModuleEdit.Text = string.Empty;
            txtInitialsEdit.Text = string.Empty;
        }

        private void ClearSubModuleFields()
        {
            txtSubModuleIdEdit.Value = string.Empty;
            txtSubModuleEdit.Text = string.Empty;
            txtPortEdit.Text = string.Empty;
        }

        private void ClearServiceFields()
        {
            txtServiceIdEdit.Value = string.Empty;
            txtServiceEdit.Text = string.Empty;
            txtServiceDescriptionEdit.Text = string.Empty;
            lbEndPointEdit.ClearSelection();
            ddlServiceApproval.ClearSelection();
        }

        private void ClearMethodFields()
        {
            txtMethodIdEdit.Value = string.Empty;
            txtMethodEdit.Text = string.Empty;
            txtDescriptionMethodEdit.Text = string.Empty;
            txtRelatedTablesEdit.Text = string.Empty;
            chkCollectionEdit.Checked = false;
            txtTypeEdit.Text = string.Empty;
            ddlMethodApproval.ClearSelection();
        }

        private void ClearParameterFields()
        {
            txtParameterIdEdit.Value = string.Empty;
            txtParameterEdit.Text = string.Empty;
            txtDescriptionParameterEdit.Text = string.Empty;
            chkCollectionParameterEdit.Checked = false;
            txtTypeParameterEdit.Text = string.Empty;
        }

        private void ClearTypeFields()
        {
            txtTypeIdEditPopup.Value = string.Empty;
            txtTypeEditPopup.Text = string.Empty;
            txtTypeDescriptionEditPopup.Text = string.Empty;
        }

        private void ClearAttributeFields()
        {
            txtAttributeIdEdit.Value = string.Empty;
            txtAttributeEdit.Text = string.Empty;
            txtDescriptionAttributeEdit.Text = string.Empty;
            txtTypeAttributeEdit.Text = string.Empty;
        }

        private void ClearAttributePopupFields()
        {
            txtAttributeIdEditPopup.Value = string.Empty;
            txtAttributeEditPopup.Text = string.Empty;
            txtDescriptionAttributeEditPopup.Text = string.Empty;
            txtTypeAttributeEditPopup.Text = string.Empty;
        }
        
        #endregion

        #region Fill Methods

        private void FillModuleSearch()
        {
            _controller.ModuleSearch = new Entity.Module();
            _controller.ModuleSearch.SubModules = new List<Entity.SubModule>();
            _controller.ModuleSearch.SubModules.Add(new Entity.SubModule());
            _controller.ModuleSearch.SubModules[0].Services = new List<Entity.Service>();
            _controller.ModuleSearch.SubModules[0].Services.Add(new Entity.Service());
            _controller.ModuleSearch.SubModules[0].Services[0].Methods = new List<Entity.Method>();
            _controller.ModuleSearch.SubModules[0].Services[0].Methods.Add(new Entity.Method());
            _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].ReturnType = new Entity.Type();
            _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].Parameters = new List<Entity.Parameter>();
            _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].Parameters.Add(new Entity.Parameter());
            _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].Parameters[0].Type = new Entity.Type();

            _controller.ModuleSearch.IsItemDivergent = chkItemDivergent.Checked;

            if (ddlModule.SelectedIndex > 0) _controller.ModuleSearch.ModuleName = ddlModule.SelectedItem.Text;
            if (ddlSubModule.SelectedIndex > 0) _controller.ModuleSearch.SubModules[0].SubModuleName = ddlSubModule.SelectedItem.Text;
            if (!string.IsNullOrEmpty(txtPort.Text)) _controller.ModuleSearch.SubModules[0].Port = Convert.ToInt32(txtPort.Text);

            _controller.ModuleSearch.SubModules[0].Services[0].ServiceName = txtService.Text;
            if (chkService.Checked) _controller.ModuleSearch.SubModules[0].Services[0].Description = txtDescription.Text;

            _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].MethodName = txtMethod.Text;
            if (chkMethod.Checked) _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].Description = txtDescription.Text;
            _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].RelatedTables = txtRelatedTables.Text;
            if (chkMethodType.Checked) _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].ReturnType.FullTypeName = txtType.Text;

            _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].Parameters[0].ParameterName = txtParameter.Text;
            if (chkParameter.Checked) _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].Parameters[0].Description = txtDescription.Text;
            if (chkParameterType.Checked) _controller.ModuleSearch.SubModules[0].Services[0].Methods[0].Parameters[0].Type.FullTypeName = txtType.Text;
        }

        private void FillEntityModule()
        {
            _controller.ModuleEdit = new Entity.Module();
            _controller.ModuleEdit.ModuleName = txtModuleEdit.Text;
            _controller.ModuleEdit.ModuleInitials = txtInitialsEdit.Text;

            if (!string.IsNullOrEmpty(txtModuleIdEdit.Value))
                _controller.ModuleEdit.ModuleId = Convert.ToInt32(txtModuleIdEdit.Value);
        }

        private void FillEntitySubModule()
        {
            _controller.SubModuleEdit = new Entity.SubModule();
            _controller.SubModuleEdit.Module = new Entity.Module();
            _controller.SubModuleEdit.Module.ModuleId = Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[1]);
            _controller.SubModuleEdit.SubModuleName = txtSubModuleEdit.Text;
            _controller.SubModuleEdit.Port = Convert.ToInt32(txtPortEdit.Text);

            if (!string.IsNullOrEmpty(txtSubModuleIdEdit.Value))
                _controller.SubModuleEdit.SubModuleId = Convert.ToInt32(txtSubModuleIdEdit.Value);
        }

        private void FillEntityService()
        {
            _controller.ServiceEdit = new Entity.Service();
            _controller.ServiceEdit.SubModule = new Entity.SubModule();
            _controller.ServiceEdit.SubModule.Module = new Entity.Module();
            _controller.ServiceEdit.SubModule.SubModuleId = Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[1]);
            _controller.ServiceEdit.SubModule.Module.ModuleId = Convert.ToInt32(trvService.SelectedNode.Parent.Value.Split('|')[1]);
            _controller.ServiceEdit.ServiceName = txtServiceEdit.Text;
            _controller.ServiceEdit.Description = txtServiceDescriptionEdit.Text;
            _controller.ServiceEdit.StatusApproval = (Entity.StatusApproval)Convert.ToInt32(ddlServiceApproval.SelectedValue);

            if (!string.IsNullOrEmpty(txtServiceIdEdit.Value))
                _controller.ServiceEdit.ServiceId = Convert.ToInt32(txtServiceIdEdit.Value);

            List<Entity.EndPoint> endPointList = new List<Entity.EndPoint>();
            for (int i = 0; i < lbEndPointEdit.Items.Count; i++)
            {
                if (lbEndPointEdit.Items[i].Selected)
                {
                    Entity.EndPoint endPoint = new Entity.EndPoint();
                    endPoint.Service = new Entity.Service();
                    endPoint.Service.ServiceId = _controller.ServiceEdit.ServiceId;
                    endPoint.Protocol = new Entity.Protocol();
                    endPoint.Protocol.ProtocolName = lbEndPointEdit.Items[i].Text;
                    endPointList.Add(endPoint);
                }
            }

            _controller.ServiceEdit.EndPoints = endPointList;
        }

        private void FillEntityMethod()
        {
            _controller.MethodEdit = new Entity.Method();
            _controller.MethodEdit.Service = new Entity.Service();
            _controller.MethodEdit.ReturnType = new Entity.Type();
            _controller.MethodEdit.Service.SubModule = new Entity.SubModule();
            _controller.MethodEdit.Service.SubModule.Module = new Entity.Module();
            _controller.MethodEdit.Service.ServiceId = Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[1]);
            _controller.MethodEdit.Service.SubModule.SubModuleId = Convert.ToInt32(trvService.SelectedNode.Parent.Value.Split('|')[1]);
            _controller.MethodEdit.Service.SubModule.Module.ModuleId = Convert.ToInt32(trvService.SelectedNode.Parent.Parent.Value.Split('|')[1]);
            _controller.MethodEdit.MethodName = txtMethodEdit.Text;
            _controller.MethodEdit.Description = txtDescriptionMethodEdit.Text;
            _controller.MethodEdit.RelatedTables = txtRelatedTablesEdit.Text;
            _controller.MethodEdit.IsReturnCollection = chkCollectionEdit.Checked;
            _controller.MethodEdit.ReturnType.FullTypeName = txtTypeEdit.Text;
            _controller.MethodEdit.StatusApproval = (Entity.StatusApproval)Convert.ToInt32(ddlMethodApproval.SelectedValue);
            
            if (!string.IsNullOrEmpty(txtMethodIdEdit.Value))
                _controller.MethodEdit.MethodId = Convert.ToInt32(txtMethodIdEdit.Value);
        }

        private void FillEntityParameter()
        {
            _controller.ParameterEdit = new Entity.Parameter();
            _controller.ParameterEdit.Type = new Entity.Type();
            _controller.ParameterEdit.Method = new Entity.Method();
            _controller.ParameterEdit.Method.Service = new Entity.Service();
            _controller.ParameterEdit.Method.Service.SubModule = new Entity.SubModule();
            _controller.ParameterEdit.Method.Service.SubModule.Module = new Entity.Module();
            _controller.ParameterEdit.Method.MethodId = Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[1]);
            _controller.ParameterEdit.Method.Service.ServiceId = Convert.ToInt32(trvService.SelectedNode.Parent.Value.Split('|')[1]);
            _controller.ParameterEdit.Method.Service.SubModule.SubModuleId = Convert.ToInt32(trvService.SelectedNode.Parent.Parent.Value.Split('|')[1]);
            _controller.ParameterEdit.Method.Service.SubModule.Module.ModuleId = Convert.ToInt32(trvService.SelectedNode.Parent.Parent.Parent.Value.Split('|')[1]);
            _controller.ParameterEdit.ParameterName = txtParameterEdit.Text;
            _controller.ParameterEdit.Description = txtDescriptionParameterEdit.Text;
            _controller.ParameterEdit.IsCollection = chkCollectionParameterEdit.Checked;
            _controller.ParameterEdit.Type.FullTypeName = txtTypeParameterEdit.Text;

            if (!string.IsNullOrEmpty(txtParameterIdEdit.Value))
                _controller.ParameterEdit.ParameterId = Convert.ToInt32(txtParameterIdEdit.Value);
        }

        private void FillEntityAttribute()
        {
            _controller.AttributeEdit = new Entity.Attribute();
            _controller.AttributeEdit.Type = new Entity.Type();
            _controller.AttributeEdit.AttributeName = _controller.IsAttributePopupVisible ? txtAttributeEditPopup.Text : txtAttributeEdit.Text;
            _controller.AttributeEdit.Description = _controller.IsAttributePopupVisible ? txtDescriptionAttributeEditPopup.Text : txtDescriptionAttributeEdit.Text;
            _controller.AttributeEdit.Type.FullTypeName = _controller.IsAttributePopupVisible ? txtTypeAttributeEditPopup.Text : txtTypeAttributeEdit.Text;

            if (_controller.IsAttributePopupVisible)            
            {
                _controller.AttributeEdit.ParentTypeId = _controller.AttributeParentTypeId;

                if (!string.IsNullOrEmpty(txtAttributeIdEditPopup.Value))
                    _controller.AttributeEdit.AttributeId = Convert.ToInt32(txtAttributeIdEditPopup.Value);
            }
            else
            {
                KindVision kindVision = (KindVision)Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[0]);

                if (kindVision == KindVision.Parameter)
                    _controller.AttributeEdit.ParentTypeId = FindTypeIdByParameterId(trvService.SelectedNode);
                else if (kindVision == KindVision.Attribute)
                    _controller.AttributeEdit.ParentTypeId = Convert.ToInt32(trvService.SelectedNode.Value.Split('|')[1]);

                if (!string.IsNullOrEmpty(txtAttributeIdEdit.Value))
                    _controller.AttributeEdit.AttributeId = Convert.ToInt32(txtAttributeIdEdit.Value);
            }
        }

        private void FillEntityType()
        {
            _controller.TypeEdit = new Entity.Type();
            _controller.TypeEdit.FullTypeName = txtTypeEditPopup.Text;
            _controller.TypeEdit.Description = txtTypeDescriptionEditPopup.Text;

            if (!string.IsNullOrEmpty(txtTypeIdEditPopup.Value))
                _controller.TypeEdit.TypeId = Convert.ToInt32(txtTypeIdEditPopup.Value);
        }

        #endregion

        #region Search Methods

        private void SearchType()
        {
            _controller.TypeSearch = new Entity.Type();
            _controller.TypeSearch.FullTypeName = txtTypeEditPopup.Text;
            _controller.SelectTypeList();
        }

        private void SearchTreeView()
        {
            FillModuleSearch();
            _controller.SelectModule();
            _controller.SelectAll();
            ShowKindVision(KindVision.Empty);
        }

        private void PopulateNodes(object[] source, TreeNodeCollection nodes, KindVision vision)
        {
            TreeNode node = null;

            switch (vision)
            {
                case KindVision.Root:

                    foreach (Entity.Module item in source)
                    {
                        node = new TreeNode(string.Format(TitleNode, item.ModuleName), string.Empty, item.IsItemDivergent ? ImageDivergent : ImageModule);
                        node.Value = Convert.ToInt32(KindVision.Module).ToString() + "|" + item.ModuleId.ToString();
                        node.PopulateOnDemand = item.SubModules.Count > 0;
                        nodes.Add(node);
                    }

                    break;
                case KindVision.Module:

                    foreach (Entity.SubModule item in source)
                    {
                        node = new TreeNode(string.Format(TitleNode, item.SubModuleName), string.Empty, item.IsItemDivergent ? ImageDivergent : ImageSubModule);
                        node.Value = Convert.ToInt32(KindVision.SubModule).ToString() + "|" + item.SubModuleId.ToString() + "|" + item.Module.ModuleId.ToString();
                        node.PopulateOnDemand = item.Services.Count > 0;
                        nodes.Add(node);
                    }

                    break;
                case KindVision.SubModule:

                    foreach (Entity.Service item in source)
                    {
                        node = new TreeNode(string.Format(TitleNode, item.ServiceName), string.Empty, item.IsItemDivergent ? ImageDivergent : ImageService);
                        node.Value = Convert.ToInt32(KindVision.Service).ToString() + "|" + item.ServiceId.ToString() + "|" + item.SubModule.SubModuleId.ToString() + "|" + item.SubModule.Module.ModuleId.ToString();
                        node.PopulateOnDemand = item.Methods.Count > 0;
                        nodes.Add(node);
                    }

                    break;
                case KindVision.Service:

                    foreach (Entity.Method item in source)
                    {
                        node = new TreeNode(string.Format(TitleNode, item.MethodName), string.Empty, item.IsItemDivergent ? ImageDivergent : ImageMethod);
                        node.Value = Convert.ToInt32(KindVision.Method).ToString() + "|" + item.MethodId.ToString() + "|" + item.Service.ServiceId.ToString() + "|" + item.Service.SubModule.SubModuleId.ToString() + "|" + item.Service.SubModule.Module.ModuleId.ToString();
                        node.PopulateOnDemand = item.Parameters.Count > 0;
                        nodes.Add(node);
                    }

                    break;
                case KindVision.Method:

                    foreach (Entity.Parameter item in source)
                    {
                        node = new TreeNode(string.Format(TitleNode, item.ParameterName), string.Empty, item.IsItemDivergent ? ImageDivergent : ImageParameter);
                        node.Value = Convert.ToInt32(KindVision.Parameter).ToString() + "|" + item.ParameterId.ToString() + "|" + item.Method.MethodId.ToString() + "|" + item.Method.Service.ServiceId.ToString() + "|" + item.Method.Service.SubModule.SubModuleId.ToString() + "|" + item.Method.Service.SubModule.Module.ModuleId.ToString();
                        node.PopulateOnDemand = EntityTypeHasAttributes(item.Type);
                        nodes.Add(node);
                    }

                    break;

                case KindVision.Parameter:

                    foreach (Entity.Attribute item in source)
                    {
                        node = new TreeNode(string.Format(TitleNode, item.AttributeName), string.Empty, item.IsItemDivergent ? ImageDivergent : ImageAttribute);
                        node.Value = Convert.ToInt32(KindVision.Attribute).ToString() + "|" + item.Type.TypeId.ToString() + "|" + item.AttributeId.ToString();
                        node.PopulateOnDemand = EntityTypeHasAttributes(item.Type);
                        nodes.Add(node);
                    }

                    break;

                case KindVision.Attribute:

                    foreach (Entity.Attribute item in source)
                    {
                        node = new TreeNode(string.Format(TitleNode, item.AttributeName), string.Empty, item.IsItemDivergent ? ImageDivergent : ImageAttribute);
                        node.Value = Convert.ToInt32(KindVision.Attribute).ToString() + "|" + item.Type.TypeId.ToString() + "|" + item.AttributeId.ToString();
                        node.PopulateOnDemand = EntityTypeHasAttributes(item.Type);
                        nodes.Add(node);
                    }

                    break;
            }
        }

        private void ShowKindVision(KindVision vision)
        {
            trModule.Visible = (vision == KindVision.Root);
            trSubModule.Visible = (vision == KindVision.Module);
            trService.Visible = (vision == KindVision.SubModule);
            trMethod.Visible = (vision == KindVision.Service);
            trParameter.Visible = (vision == KindVision.Method);
            trAttribute.Visible = ((vision == KindVision.Parameter) || (vision == KindVision.Attribute));
        }

        private TreeNode FindNode(string value)
        {
            TreeNode node = null;

            foreach (TreeNode item in trvService.SelectedNode.ChildNodes)
            {
                if (item.Value.Split('|')[1] == value)
                {
                    node = item;
                    continue;
                }
            }

            return node;
        }

        private TreeNode FindNode(string value, TreeNode parentNode)
        {
            TreeNode node = null;

            foreach (TreeNode item in parentNode.ChildNodes)
            {
                if (item.Value.Split('|')[1] == value)
                {
                    node = item;
                    continue;
                }
                else
                    node = FindNode(value, item);
            }

            return node;
        }

        private int FindTypeIdByParameterId(TreeNode node)
        {
            int id = 0;

            if (_controller.TreeViewList != null && node != null)
            {
                if ((KindVision)Convert.ToInt32(node.Value.Split('|')[0]) == KindVision.Parameter)
                {
                    id = _controller.TreeViewList.Find(m => m.ModuleId == Convert.ToInt32(node.Value.Split('|')[5])).
                            SubModules.Find(s => s.SubModuleId == Convert.ToInt32(node.Value.Split('|')[4])).
                                Services.Find(s => s.ServiceId == Convert.ToInt32(node.Value.Split('|')[3])).
                                    Methods.Find(m => m.MethodId == Convert.ToInt32(node.Value.Split('|')[2])).
                                        Parameters.Find(m => m.ParameterId == Convert.ToInt32(node.Value.Split('|')[1])).Type.TypeId.Value;
                }
            }
            
            return id;
        }

        private Entity.Parameter FindParameterInTreeView(TreeNode node)
        {
            Entity.Parameter parameter = null;

            if (_controller.TreeViewList != null && node != null)
            {
                if ((KindVision)Convert.ToInt32(node.Value.Split('|')[0]) == KindVision.Parameter)
                {
                    parameter = _controller.TreeViewList.Find(m => m.ModuleId == Convert.ToInt32(node.Value.Split('|')[5])).
                                    SubModules.Find(s => s.SubModuleId == Convert.ToInt32(node.Value.Split('|')[4])).
                                        Services.Find(s => s.ServiceId == Convert.ToInt32(node.Value.Split('|')[3])).
                                            Methods.Find(m => m.MethodId == Convert.ToInt32(node.Value.Split('|')[2])).
                                                Parameters.Find(m => m.ParameterId == Convert.ToInt32(node.Value.Split('|')[1]));
                }
            }

            return parameter;
        }

        private Entity.Method FindMethodInTreeView(TreeNode node)
        {
            Entity.Method method = null;

            if (_controller.TreeViewList != null && node != null)
            {
                if ((KindVision)Convert.ToInt32(node.Value.Split('|')[0]) == KindVision.Parameter)
                {
                    method = _controller.TreeViewList.Find(m => m.ModuleId == Convert.ToInt32(node.Value.Split('|')[5])).
                                SubModules.Find(s => s.SubModuleId == Convert.ToInt32(node.Value.Split('|')[4])).
                                    Services.Find(s => s.ServiceId == Convert.ToInt32(node.Value.Split('|')[3])).
                                        Methods.Find(m => m.MethodId == Convert.ToInt32(node.Value.Split('|')[2]));
                }
            }

            return method;
        }

        private bool EntityTypeHasAttributes(Entity.Type type)
        {
            List<Entity.Attribute> list = null;
            bool bRet = false;

            if (_controller.AttributeListStatic != null)
            {
                list = _controller.AttributeListStatic.FindAll(t => t.ParentTypeId == type.TypeId);

                if (list != null)
                    bRet = list.Count > 0;
            }
            return bRet;
        }

        private void ShowModalAttribute()
        {
            ScriptManager.RegisterStartupScript(Page, ScriptManagerService.GetType(), "Show", "ShowModal('divAttribute');", true);
        }

        private void ShowModalType()
        {
            ScriptManager.RegisterStartupScript(Page, ScriptManagerService.GetType(), "Show", "ShowModal('divType');", true);
        }

        #endregion

        #region Insert Methods

        private void InsertModule()
        {
            FillEntityModule();

            if (_controller.InsetModule())
            {
                TreeNode node = new TreeNode(string.Format(TitleNode, _controller.ModuleEdit.ModuleName), string.Empty, ImageModule);
                node.Value = Convert.ToInt32(KindVision.Module).ToString() + "|" + _controller.ModuleEdit.ModuleId.ToString();
                trvService.SelectedNode.ChildNodes.Add(node);

                ClearModuleFields();
            }
        }

        private void InsertSubModule()
        {
            FillEntitySubModule();

            if (_controller.InsetSubModule())
            {
                TreeNode node = new TreeNode(string.Format(TitleNode, _controller.SubModuleEdit.SubModuleName), string.Empty, ImageSubModule);
                node.Value = Convert.ToInt32(KindVision.SubModule).ToString() + "|" + _controller.SubModuleEdit.SubModuleId.ToString() + "|" + _controller.SubModuleEdit.Module.ModuleId.ToString();
                trvService.SelectedNode.ChildNodes.Add(node);

                ClearSubModuleFields();
            }
        }

        private void InsertService()
        {
            FillEntityService();

            if (_controller.InsetService())
            {
                TreeNode node = new TreeNode(string.Format(TitleNode, _controller.ServiceEdit.ServiceName), string.Empty, ImageService);
                node.Value = Convert.ToInt32(KindVision.Service).ToString() + "|" + _controller.ServiceEdit.ServiceId.ToString() + "|" + _controller.ServiceEdit.SubModule.SubModuleId.ToString() + "|" + _controller.ServiceEdit.SubModule.Module.ModuleId.ToString();
                trvService.SelectedNode.ChildNodes.Add(node);

                ClearServiceFields();
            }
        }

        private void InsertMethod()
        {
            FillEntityMethod();

            if (_controller.InsetMethod())
            {
                TreeNode node = new TreeNode(string.Format(TitleNode, _controller.MethodEdit.MethodName), string.Empty, ImageMethod);
                node.Value = Convert.ToInt32(KindVision.Method).ToString() + "|" + _controller.MethodEdit.MethodId.ToString() + "|" + _controller.MethodEdit.Service.ServiceId.ToString() + "|" + _controller.MethodEdit.Service.SubModule.SubModuleId.ToString() + "|" + _controller.MethodEdit.Service.SubModule.Module.ModuleId.ToString();
                trvService.SelectedNode.ChildNodes.Add(node);

                ClearMethodFields();
            }
        }

        private void InsertParameter()
        {
            FillEntityParameter();

            if (_controller.InsetParameter())
            {
                TreeNode node = new TreeNode(string.Format(TitleNode, _controller.ParameterEdit.ParameterName), string.Empty, ImageParameter);
                node.Value = Convert.ToInt32(KindVision.Parameter).ToString() + "|" + _controller.ParameterEdit.ParameterId.ToString() + "|" + _controller.ParameterEdit.Method.MethodId.ToString() + "|" + _controller.ParameterEdit.Method.Service.ServiceId.ToString() + "|" + _controller.ParameterEdit.Method.Service.SubModule.SubModuleId.ToString() + "|" + _controller.ParameterEdit.Method.Service.SubModule.Module.ModuleId.ToString();

                if (_controller.ParameterEdit.Type.TypeId != 0)
                    node.PopulateOnDemand = EntityTypeHasAttributes(_controller.ParameterEdit.Type);

                trvService.SelectedNode.ChildNodes.Add(node);

                ClearParameterFields();
            }
        }

        private void InsertAttribute()
        {
            FillEntityAttribute();

            if (_controller.InsetAttribute())
            {
                TreeNode node = new TreeNode(string.Format(TitleNode, _controller.AttributeEdit.AttributeName), string.Empty, ImageAttribute);
                node.Value = Convert.ToInt32(KindVision.Attribute).ToString() + "|" + _controller.AttributeEdit.AttributeId.ToString();

                if (_controller.AttributeEdit.Type.TypeId != 0)
                    node.PopulateOnDemand = EntityTypeHasAttributes(_controller.AttributeEdit.Type);

                trvService.SelectedNode.ChildNodes.Add(node);

                ClearAttributeFields();
            }
        }

        private void InsertType()
        {
            FillEntityType();

            if (_controller.InsetType())
                ClearTypeFields();
        }

        #endregion

        #region Update Methods

        private void UpdateModule()
        {
            TreeNode node = null;

            FillEntityModule();

            _controller.UpdateModule();

            node = FindNode(_controller.ModuleEdit.ModuleId.ToString());

            if (node != null)
                node.Text = string.Format(TitleNode, _controller.ModuleEdit.ModuleName);

            ClearModuleFields();
        }

        private void UpdateSubModule()
        {
            TreeNode node = null;

            FillEntitySubModule();

            _controller.UpdateSubModule();

            node = FindNode(_controller.SubModuleEdit.SubModuleId.ToString());

            if (node != null)
                node.Text = string.Format(TitleNode, _controller.SubModuleEdit.SubModuleName);

            ClearSubModuleFields();
        }

        private void UpdateService()
        {
            TreeNode node = null;

            FillEntityService();

            _controller.UpdateService();

            node = FindNode(_controller.ServiceEdit.ServiceId.ToString());

            if (node != null)
                node.Text = string.Format(TitleNode, _controller.ServiceEdit.ServiceName);

            ClearServiceFields();
        }

        private void UpdateMethod()
        {
            TreeNode node = null;

            FillEntityMethod();

            _controller.UpdateMethod();

            node = FindNode(_controller.MethodEdit.MethodId.ToString());

            if (node != null)
                node.Text = string.Format(TitleNode, _controller.MethodEdit.MethodName);
            
            ClearMethodFields();
        }

        private void UpdateParameter()
        {
            TreeNode node = null;

            FillEntityParameter();

            _controller.UpdateParameter();

            node = FindNode(_controller.ParameterEdit.ParameterId.ToString());

            if (node != null)
            {
                node.Text = string.Format(TitleNode, _controller.ParameterEdit.ParameterName);

                Entity.Parameter parameter = FindParameterInTreeView(node);

                if (parameter != null)
                {
                    parameter.Type = _controller.ParameterEdit.Type;

                    if (node.ChildNodes.Count > 0)
                    {
                        node.ChildNodes.Clear();

                        PopulateNodes(_controller.AttributeListStatic.FindAll(t => t.ParentTypeId == parameter.Type.TypeId).ToArray(),
                                        node.ChildNodes,
                                        (KindVision)Convert.ToInt32(node.Value.Split('|')[0]));
                    }
                }
            }

            ClearParameterFields();
        }

        private void UpdateAttribute()
        {
            TreeNode node = null;

            FillEntityAttribute();

            _controller.UpdateAttribute();

            node = FindNode(_controller.AttributeEdit.AttributeId.ToString());

            if (node != null)
                node.Text = string.Format(TitleNode, _controller.AttributeEdit.AttributeName);

            if (!_controller.IsAttributePopupVisible)
                ClearAttributeFields();
            else
                ClearAttributePopupFields();
        }

        private void UpdateType()
        {
            FillEntityType();

            _controller.UpdateType();
            
            ClearTypeFields();
        }

        #endregion

        #region Delete Methods

        private void DeleteModule(int index)
        {
            TreeNode node = null;

            _controller.ModuleEdit = new Entity.Module();
            _controller.ModuleEdit.ModuleId = Convert.ToInt32(gdvModule.DataKeys[index].Value);

            _controller.DeleteModule();

            node = FindNode(_controller.ModuleEdit.ModuleId.ToString());

            if (node != null)
                node.Parent.ChildNodes.Remove(node);
        }

        private void DeleteSubModule(int index)
        {
            TreeNode node = null;

            _controller.SubModuleEdit = new Entity.SubModule();
            _controller.SubModuleEdit.SubModuleId = Convert.ToInt32(gdvSubModule.DataKeys[index].Value);

            _controller.DeleteSubModule();

            node = FindNode(_controller.SubModuleEdit.SubModuleId.ToString());

            if (node != null)
                node.Parent.ChildNodes.Remove(node);
        }

        private void DeleteService(int index)
        {
            TreeNode node = null;

            _controller.ServiceEdit = new Entity.Service();
            _controller.ServiceEdit.ServiceId = Convert.ToInt32(gdvService.DataKeys[index].Value);

            _controller.DeleteService();

            node = FindNode(_controller.ServiceEdit.ServiceId.ToString());

            if (node != null)
                node.Parent.ChildNodes.Remove(node);
        }

        private void DeleteMethod(int index)
        {
            TreeNode node = null;

            _controller.MethodEdit = new Entity.Method();
            _controller.MethodEdit.MethodId = Convert.ToInt32(gdvMethod.DataKeys[index].Value);

            _controller.DeleteMethod();

            node = FindNode(_controller.MethodEdit.MethodId.ToString());

            if (node != null)
                node.Parent.ChildNodes.Remove(node);
        }

        private void DeleteParameter(int index)
        {
            TreeNode node = null;

            _controller.ParameterEdit = new Entity.Parameter();
            _controller.ParameterEdit.ParameterId = Convert.ToInt32(gdvParameter.DataKeys[index].Value);

            _controller.DeleteParameter();

            node = FindNode(_controller.ParameterEdit.ParameterId.ToString());

            if (node != null)
                node.Parent.ChildNodes.Remove(node);
        }

        private void DeleteAttribute(int index)
        {
            TreeNode node = null;

            _controller.AttributeEdit = new Entity.Attribute();
            _controller.AttributeEdit.AttributeId = _controller.IsAttributePopupVisible ? Convert.ToInt32(gdvAttribute.DataKeys[index].Value) : Convert.ToInt32(gdvAttributePopup.DataKeys[index].Value);

            _controller.DeleteAttribute();

            node = FindNode(_controller.AttributeEdit.AttributeId.ToString(), trvService.SelectedNode);

            if (node != null)
                node.Parent.ChildNodes.Remove(node);
        }

        private void DeleteType(int index)
        {
            _controller.TypeEdit = new Entity.Type();
            _controller.TypeEdit.TypeId = Convert.ToInt32(gdvTypePopup.DataKeys[index].Value);

            _controller.DeleteType();
        }

        #endregion

        #region IServiceController Members

        void IServiceController.ShowTreeView()
        {
            trvService.Nodes.Clear();

            trvService.Nodes.Add(new TreeNode(string.Format(TitleNode, TitleNodeRoot), Convert.ToInt32(KindVision.Root).ToString() + "|" + 1.ToString(), ImageRoot));

            if (_controller.TreeViewList != null)
                PopulateNodes(_controller.TreeViewList.ToArray(), trvService.Nodes[0].ChildNodes, KindVision.Root);

            trvService.Nodes[0].Expand();
        }

        void IServiceController.ShowModule()
        {
            ddlModule.DataTextField = "ModuleName";
            ddlModule.DataValueField = "ModuleId";
            ddlModule.DataSource = _controller.Module;
            ddlModule.DataBind();
            ddlModule.Items.Insert(0, new ListItem(ItemDropDownList));
            ddlModule.SelectedIndex = 0;
            ddlSubModule.Items.Clear();
            ddlSubModule.Items.Insert(0, new ListItem(ItemDropDownList));
        }

        void IServiceController.ShowSubModule()
        {
            ddlSubModule.DataTextField = "SubModuleName";
            ddlSubModule.DataValueField = "SubModuleId";
            ddlSubModule.DataSource = _controller.SubModule;
            ddlSubModule.DataBind();
            ddlSubModule.Items.Insert(0, new ListItem(ItemDropDownList));
        }

        void IServiceController.ShowProtocol()
        {
            this.lbEndPointEdit.DataTextField = "ProtocolName";
            this.lbEndPointEdit.DataValueField = "ProtocolName";
            this.lbEndPointEdit.DataSource = _controller.Protocol;
            this.lbEndPointEdit.DataBind();
        }

        void IServiceController.ShowModuleList()
        {
            gdvModule.DataSource = _controller.ModuleList;
            gdvModule.DataBind();
        }

        void IServiceController.ShowSubModuleList()
        {
            gdvSubModule.DataSource = _controller.SubModuleList;
            gdvSubModule.DataBind();
        }

        void IServiceController.ShowSubModulePort()
        {
            txtPortEdit.Text = _controller.SubModulePort.ToString();
        }

        void IServiceController.ShowServiceList()
        {
            gdvService.DataSource = _controller.ServiceList;
            gdvService.DataBind();
        }

        void IServiceController.ShowMethodList()
        {
            gdvMethod.DataSource = _controller.MethodList;
            gdvMethod.DataBind();
        }

        void IServiceController.ShowParameterList()
        {
            gdvParameter.DataSource = _controller.ParameterList;
            gdvParameter.DataBind();
        }

        void IServiceController.ShowTypeList()
        {
            gdvTypePopup.DataSource = _controller.TypeList;
            gdvTypePopup.DataBind();
        }

        void IServiceController.ShowAttributeList()
        {
            if (_controller.IsAttributePopupVisible)
            {
                gdvAttributePopup.DataSource = _controller.AttributeList;
                gdvAttributePopup.DataBind();
            }
            else
            {
                gdvAttribute.DataSource = _controller.AttributeList;
                gdvAttribute.DataBind();
            }
        }

        void IServiceController.ShowMessageError()
        {
            ScriptManager.RegisterStartupScript(Page, ScriptManagerService.GetType(), "Alert", InsertMessage, true);
        }

        #endregion

        #endregion
    }
}